package com.zhang;

import java.io.*;

public class ConvertDat {
    public static void main(String[] args) throws Exception {
        String srcPath = "D:\\wechat\\WeChat Files\\Aiyou_a3a4\\FileStorage\\Image\\2023-12";//源文件路径
        String outPath = "D:\\Desktop\\转换\\2023-11";//解码后输出路径

        File file = new File(srcPath);
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".dat");
            }
        });
        assert files != null;
        for (File f : files) {
            InputStream in = new FileInputStream(f);
            int read = in.read();
            int xorResult = read ^ 0xFF;
            in = new FileInputStream(f);//重新获取输入流
            OutputStream out = new FileOutputStream(outPath + f.getName()
                    .replace(".dat", ".png"));
            byte[] buffer = new byte[1024];
            int r;
            byte[] outBuffer = new byte[1024];
            while ((r = in.read(buffer)) > 0) {
                for (int i = 0; i < r; i++) {
                    byte b = buffer[i];
                    int c = b ^ xorResult;//与计算出的结果异或
                    Byte aByte = (byte) c;
                    outBuffer[i] = aByte;
                }
                out.write(outBuffer, 0, r);
                out.flush();
            }
            in.close();
            out.close();
        }
    }
}
